﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace BanHangCshap.Class
{
    class DonDatHang:Database
    {
        #region ***** Fields & Properties *****
        private string _maddh;
        public string maddh
        {
            get
            {
                return _maddh;
            }
            set
            {
                _maddh = value;
            }
        }
        private string _ngaydathang;
        public string ngaydathang
        {
            get
            {
                return _ngaydathang;
            }
            set
            {
                _ngaydathang = value;
            }
        }
        private string _ngayphaigiao;
        public string ngayphaigiao
        {
            get
            {
                return _ngayphaigiao;
            }
            set
            {
                _ngayphaigiao = value;
            }
        }
        private int _makhncc;
        public int makhncc
        {
            get
            {
                return _makhncc;
            }
            set
            {
                _makhncc = value;
            }
        }
        private double _tongtienchietkhau;
        public double tongtienchietkhau
        {
            get
            {
                return _tongtienchietkhau;
            }
            set
            {
                _tongtienchietkhau = value;
            }
        }
        private double _tongtienthue;
        public double tongtienthue
        {
            get
            {
                return _tongtienthue;
            }
            set
            {
                _tongtienthue = value;
            }
        }
        private double _tongtienso;
        public double tongtienso
        {
            get
            {
                return _tongtienso;
            }
            set
            {
                _tongtienso = value;
            }
        }
        private string _tongtienchu;
        public string tongtienchu
        {
            get
            {
                return _tongtienchu;
            }
            set
            {
                _tongtienchu = value;
            }
        }
        private int _loaiddh;
        public int loaiddh
        {
            get
            {
                return _loaiddh;
            }
            set
            {
                _loaiddh = value;
            }
        }
        private string _tendn;
        public string tendn
        {
            get
            {
                return _tendn;
            }
            set
            {
                _tendn = value;
            }
        }
        #endregion

        #region ***** Init Methods *****
        public DonDatHang()
        {
        }
        public DonDatHang(string maddh)
        {
            this.maddh = maddh;
        }
        public DonDatHang(string maddh, string ngaydathang, int makhncc, double tongtienchietkhau, double tongtienthue, double tongtienso, string tongtienchu, int loaiddh, string tendn, string ngayphaigiao)
        {
            this.maddh = maddh;
            this.ngaydathang = ngaydathang;
            this.makhncc = makhncc;
            this.tongtienchietkhau = tongtienchietkhau;
            this.tongtienthue = tongtienthue;
            this.tongtienso = tongtienso;
            this.tongtienchu = tongtienchu;
            this.loaiddh = loaiddh;
            this.tendn = tendn;
            this.ngayphaigiao = ngayphaigiao;
        }
        #endregion
        //public Boolean themDonDatHang()
        //{
        //    string sql = 
        //}
        public DataTable layDSDonDatHangChuaGiaoDuSL(int loaiddh)
        {
            string sql = "select a.maddh,ngaydathang,a.makhncc,tenkhncc,tongtienchietkhau, tongtienthue,tongtienso,ngayphaigiao from DonDatHang a, KhachHangNCC b where a.makhncc = b.makhncc and maddh in ";
            sql += "(select distinct  maddh ";
            sql += "from DonDatHangCT ";
            sql += "where sldagiao < sldathang)  and loaiddh = " + loaiddh + " ";
            sql += "order by ngayphaigiao desc";
            return getDataTable(sql);
        }
        public DataTable layDSDonDatHang(int loaiddh)
        {
            string sql = "select a.maddh, tenhang,b.dvt,dgdathang,sldathang,thanhtien,b.chietkhau, thanhtienchietkhau,b.thue,thanhtienthue,sldagiao,thanhtien - thanhtienchietkhau + thanhtienthue as tongtien from DonDatHang a, DonDatHangCT b, HangHoa c where a.maddh = b.maddh and b.mahang = c.mahang and loaiddh = " + loaiddh + "";
            return getDataTable(sql);
        }
        public DataTable layThongTinDonDatHang(string maddh)
        {
            string sql = "select * from DonDatHang where maddh = '" + maddh + "'";
            //string sql = "select mahang,tenhang,dvt,giaxuatle from HangHoa";
            return getDataTable(sql);
        }
        public DataTable layDSDonDatHangCT(string maddh)
        {
           string sql = "select a.mahang, tenhang,a.dvt,dgdathang,sldathang,thanhtien,a.chietkhau, thanhtienchietkhau,a.thue,thanhtienthue,sldagiao,thanhtien - thanhtienchietkhau + thanhtienthue as tongtien from DonDatHangCT a, HangHoa b, LoaiHang c where maddh = '" + maddh + "' and a.mahang = b.mahang and b.maloai = c.maloai";
            //string sql = "select mahang,tenhang,dvt,giaxuatle from HangHoa";
            return getDataTable(sql);
        }
        public string laySoDDHTrongNgay(string ngay)
        {
            string sql = "SELECT max(convert(int,substring(maddh,12,len(maddh)-10))) FROM DonDatHang WHERE convert(varchar(10),ngaydathang,103)='" + ngay + "' and left(maddh,2) = 'DH'  ";
            return getString(sql);
        }
        public Boolean themDonDatHang()
        {
            string sql = "insert into DonDatHang(maddh,ngaydathang,makhncc,tongtienchietkhau,tongtienthue,tongtienso,tongtienchu,loaiddh,ngayphaigiao,tendn) ";
            sql += "values ('" + maddh + "','" + ngaydathang + "'," + makhncc + "," + tongtienchietkhau + "," + tongtienthue + "," + tongtienso + ",N'" + tongtienchu + "'," + loaiddh + ",'" + ngayphaigiao + "','" + tendn + "')";
            return executeQueryTrans(sql);
        }



    }
}
